package cn.tedu.ymg.product.service.impl;

import cn.tedu.ymg.commons.ex.ServiceException;
import cn.tedu.ymg.commons.pojo.vo.PageData;
import cn.tedu.ymg.commons.util.PageInfoToPageDataConverter;
import cn.tedu.ymg.commons.web.ServiceCode;
import cn.tedu.ymg.product.mapper.CategoryMapper;
import cn.tedu.ymg.product.pojo.entity.Category;
import cn.tedu.ymg.product.pojo.param.CategoryAddNewParam;
import cn.tedu.ymg.product.pojo.param.CategoryUpdateInfoParam;
import cn.tedu.ymg.product.pojo.vo.CategoryListItemVO;
import cn.tedu.ymg.product.pojo.vo.CategoryStandardVO;
import cn.tedu.ymg.product.service.ICategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class CategoryServiceImpl implements ICategoryService {

    @Autowired
    CategoryMapper CategoryMapper;



    @Override
    public void addNew(CategoryAddNewParam categoryAddNewParam) {

        log.debug("开始处理【添加方案类型的业务】，参数", categoryAddNewParam);

        //将方案类型写入到数据库中
        Category Category = new Category();
        BeanUtils.copyProperties(categoryAddNewParam, Category);

        int rows = CategoryMapper.insert(Category);
        if (rows !=1){
            String Category1 = "添加方案类型失败，服务器忙，请稍后再试！";
            log.warn(Category1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Category1);
        }
        log.debug("将新的方案类型数据写入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除方案类型】的业务，参数：{}", id);
        Category Category = CategoryMapper.selectById(id);
        if (Category ==null){
            String Category1 = "删除方案类型失败，Id不存在";
            log.warn(Category1);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Category1);
        }

        int rows = CategoryMapper.deleteById(id);
        if (rows!=1){
            String Category1 = "删除方案类型失败，服务器忙，请稍后再试！";
            log.warn(Category1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Category1);
        }
        log.debug("从数据库删除方案类型，完成！");
    }

    @Override
    public void updateInfoById(Long id, CategoryUpdateInfoParam categoryUpdateInfoParam) {

        log.debug("开始处理【修改方案类型】的业务，ID:{},新数据：{}", id, categoryUpdateInfoParam);

        //检查方案类型是否存在，如果不存在，则抛出异常
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = CategoryMapper.selectCount(queryWrapper);
        log.debug("根据示例ID统计匹配的方案类型数量，结果：{}", countById);
        if (countById == 0) {
            String Category = "修改方案类型失败，方案类型数据不存在！";
            log.warn(Category);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, Category);
        }

        //执行修改
        Category Category = new Category();
        BeanUtils.copyProperties(categoryUpdateInfoParam, Category);
        Category.setId(id);
        int rows = CategoryMapper.updateById(Category);
        if (rows!=1){
            String Category1 = "修改方案类型失败，服务器忙，请稍后再试！";
            log.warn(Category1);
            throw new ServiceException(ServiceCode.ERR_INSERT, Category1);
        }
        log.debug("从数据库修改方案类型，完成！");
    }

    @Override
    public CategoryStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询方案类型】的业务，参数：{}", id);
        CategoryStandardVO queryResult = CategoryMapper.getStandardById(id);
        if (queryResult==null){
            String Category = "查询方案类型失败，方案类型数据不存在！";
            log.warn(Category);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,Category);
        }
        return queryResult;
    }

    @Override
    public PageData<CategoryListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum,pageSize);
    }

    @Override
    public PageData<CategoryListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询方案类型列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<CategoryListItemVO> list =  CategoryMapper.list();
        PageInfo<CategoryListItemVO> pageInfo = new PageInfo<>(list);
        PageData<CategoryListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
